package console

import (
	"WIKIHepler_PCR/database"
	"fmt"
	"time"

	tui "github.com/marcusolsson/tui-go"
)

func event_drawing() {

	title.SetText("WIKIHepler_PCR")

	library = tui.NewTable(0, 0)
	library.SetColumnStretch(0, 1)
	library.SetColumnStretch(1, 1)
	library.SetColumnStretch(2, 2)
	library.SetColumnStretch(3, 2)
	library.SetColumnStretch(4, 4)
	library.SetFocused(true)

	library.AppendRow(
		tui.NewLabel("No."),
		tui.NewLabel("活动ID"),
		tui.NewLabel("开始时间"),
		tui.NewLabel("结束时间"),
		tui.NewLabel("活动名称"),
	)

	event_getdata()
	status.SetText("按下F1获取帮助")
	status.SetPermanentText(fmt.Sprintf("Page %d,PageSize %d", page, pagesize))

	root = tui.NewVBox(
		title,
		library,
		tui.NewSpacer(),
		progress,
		status,
	)
	ui.SetWidget(root)
	ui.Repaint()
	event_key()
}

//主界面快捷键绑定
func event_key() {
	ui.ClearKeybindings()
	ui.SetKeybinding("F1", event_helper)
	ui.SetKeybinding("H", event_helper)
	//ui.SetKeybinding("Enter", event_enterPress)
	ui.SetKeybinding("Esc", main_back)
	ui.SetKeybinding("F5", func() { ui.Repaint() })
	//上一页
	ui.SetKeybinding("Left", func() {
		if page != 1 {
			page = page - 1
		}
		event_drawing()
	})
	//下一页
	ui.SetKeybinding("Right", func() {
		if len(objs) == pagesize {
			page = page + 1
		}
		event_drawing()
	})
	//删除数据
	ui.SetKeybinding("D", func() {
		op = library.Selected()
		if op == 0 {
			return
		}
		t := objs[op-1].E_id
		database.EVENT_DelOne(t)
		event_drawing()
	})

	//插入数据
	ui.SetKeybinding("I", event_insert)

	//模板插入数据
	ui.SetKeybinding("P", tplte_drawing)

	//导出可用数据
	ui.SetKeybinding("X", func() {
		Do_get_output()
		status.SetText("event.json已成功生成")
		ui.Repaint()
	})

	//增加分页
	ui.SetKeybinding("=", func() {
		pagesize = pagesize + 1
		event_drawing()
	})

	//缩减分页
	ui.SetKeybinding("-", func() {
		if pagesize != 5 {
			pagesize = pagesize - 1
		}
		event_drawing()
	})
}

//活动日历帮助
func event_helper() {
	title.SetText("活动日历数据管理")

	library = tui.NewTable(0, 0)
	library.SetColumnStretch(0, 1)
	library.SetColumnStretch(1, 1)
	library.SetColumnStretch(2, 4)

	library.AppendRow(
		tui.NewLabel("按键F1"),
		tui.NewLabel(""),
		tui.NewLabel("查看帮助内容"),
	)
	library.AppendRow(
		tui.NewLabel("按键D"),
		tui.NewLabel(""),
		tui.NewLabel("删除指定行的数据"),
	)

	library.AppendRow(
		tui.NewLabel("按键X"),
		tui.NewLabel(""),
		tui.NewLabel("根据当前日期输出event.json"),
	)
	library.AppendRow(
		tui.NewLabel("按键I"),
		tui.NewLabel(""),
		tui.NewLabel("插入新数据"),
	)
	library.AppendRow(
		tui.NewLabel("按键P"),
		tui.NewLabel(""),
		tui.NewLabel("利用模板快速插入新数据（推荐）"),
	)
	library.AppendRow(
		tui.NewLabel("按键『=+』"),
		tui.NewLabel(""),
		tui.NewLabel("增加分页"),
	)
	library.AppendRow(
		tui.NewLabel("按键『-_』"),
		tui.NewLabel(""),
		tui.NewLabel("缩减分页"),
	)
	library.AppendRow(
		tui.NewLabel("按键Left Right"),
		tui.NewLabel(""),
		tui.NewLabel("切页/退出帮助"),
	)

	root = tui.NewVBox(
		title,
		library,
		tui.NewSpacer(),
		progress,
		status,
	)
	ui.SetWidget(root)
	ui.Repaint()

}

var (
	op       int
	page     = 1
	pagesize = 7
	objs     []*database.EVENT
)

//获取数据
func event_getdata() {
	objs = database.EVENT_Page(page, pagesize)
	for k, v := range objs {
		library.AppendRow(
			tui.NewLabel(fmt.Sprintf("%d", k)),
			tui.NewLabel(fmt.Sprintf("%d", v.E_id)),
			tui.NewLabel(time.Unix(v.E_start, 0).Format("2006/01/02")),
			tui.NewLabel(time.Unix(v.E_end, 0).Format("2006/01/02")),
			tui.NewLabel(v.E_name),
		)
	}
}

var (
	entry_name *tui.Entry
	entry_s    *tui.Entry
	entry_e    *tui.Entry
)

//插入新数据
func event_insert() {
	title.SetText("插入新数据")
	l1 := tui.NewLabel("事件名称")
	entry_name = tui.NewEntry()
	op = 0
	entry_name.SetFocused(true)
	l2 := tui.NewLabel("开始时间(YYYY/MM/DD)")
	entry_s = tui.NewEntry()
	l3 := tui.NewLabel("结束时间(YYYY/MM/DD)")
	entry_e = tui.NewEntry()
	root = tui.NewVBox(
		title,
		l1,
		entry_name,
		l2,
		entry_s,
		l3,
		entry_e,
	)
	ui.SetWidget(root)
	ui.Repaint()
	insert_key()
}

func insert_key() {
	ui.ClearKeybindings()
	//上移焦点
	ui.SetKeybinding("Up", func() {
		if op != 0 {
			op = op - 1
		}
		focusE(op)
	})
	//下移焦点
	ui.SetKeybinding("Down", func() {
		if op != 2 {
			op = op + 1
		}
		focusE(op)
	})

	//添加数据
	ui.SetKeybinding("Enter", func() {
		e_name := entry_name.Text()
		e_start := entry_s.Text()
		e_end := entry_e.Text()

		time_start, err := time.Parse("2006/01/02", e_start)
		if err != nil {
			title.SetText("开始时间不符合要求(YYYY/MM/DD)" + err.Error())
			ui.Repaint()
			return
		}
		time_end, err := time.Parse("2006/01/02", e_end)
		if err != nil {
			title.SetText("结束时间不符合要求(YYYY/MM/DD)" + err.Error())
			ui.Repaint()
			return
		}

		var e database.EVENT
		e.E_name = e_name
		e.E_start = time_start.Unix()
		e.E_end = time_end.Unix()

		if e.E_start > e.E_end {
			title.SetText("错误，开始时间晚于结束时间")
			ui.Repaint()
			return
		}

		aft := database.EVENT_AddOne(e)
		if aft > 0 {
			event_drawing()
		}
	})

	//退出添加
	ui.SetKeybinding("Esc", event_drawing)
}

//切换输入焦点
func focusE(op int) {
	entry_name.SetFocused(false)
	entry_s.SetFocused(false)
	entry_e.SetFocused(false)
	if op == 0 {
		entry_name.SetFocused(true)
	}
	if op == 1 {
		entry_s.SetFocused(true)
	}
	if op == 2 {
		entry_e.SetFocused(true)
	}
}
